package com.test.daily.leetcode.y2021.m10.day1009;

import java.util.*;

/**
 * @descriptions: SummaryRanges
 * @author: Tom
 * @date: 2021/10/9 上午 10:47
 * @version: 1.0
 */
public class Code01_SummaryRanges {


    public static void main(String[] args) {
        Code01_SummaryRanges cs = new Code01_SummaryRanges();
        cs.addNum(1);      // arr = [1]
        cs.print(cs.getIntervals()); // 返回 [[1, 1]]
        cs.addNum(0);      // arr = [1, 3]
        cs.print(cs.getIntervals()); // 返回 [[1, 1], [3, 3]]
//        cs.addNum(7);      // arr = [1, 3, 7]
//        cs.print(cs.getIntervals());// 返回 [[1, 1], [3, 3], [7, 7]]
//        cs.addNum(2);      // arr = [1, 2, 3, 7]
//        cs.print(cs.getIntervals()); // 返回 [[1, 3], [7, 7]]
//        cs.addNum(6);      // arr = [1, 2, 3, 6, 7]
        cs.print(cs.getIntervals()); // 返回 [[1, 3], [6, 7]]
    }


    public static class Node {
        public int start;
        public int end;
        public Node(int start, int end) {
            this.start = start;
            this.end = end;
        }
    }

    public TreeMap<Integer, Node> resultMap;

    public Code01_SummaryRanges() {
        this.resultMap = new TreeMap<>();
    }

    public void addNum(int val) {
        if (this.resultMap.containsKey(val)) {
            return;
        }
        Integer lower = this.resultMap.lowerKey(val);
        if (lower == null) {
            if(resultMap.containsKey(val + 1)){
                resultMap.put(val, new Node(val, resultMap.get(val + 1).end));
                resultMap.remove(resultMap.get(val + 1).start);
            }else{
                resultMap.put(val, new Node(val, val));
            }
            return ;
        } else {
            Node lowerNode = this.resultMap.lowerEntry(val).getValue();
            if (lowerNode.end >= val) {
                return;
            }
            // 拓展长度，不能连成一片的,但是可以增加的
            if (lowerNode.end + 1 == val && !resultMap.containsKey(val + 1)) {
                resultMap.put(lowerNode.start, new Node(lowerNode.start, val));
                return;
            }
            // 拓展长度，可以连成一片的
            if (lowerNode.end + 1 == val && resultMap.containsKey(val + 1)) {
                resultMap.put(lowerNode.start,
                        new Node(lowerNode.start, resultMap.get(val + 1).end));
                resultMap.remove(resultMap.get(val + 1).start);
                return;
            }
            // 独立的
            if (lowerNode.end < val && !resultMap.containsKey(val + 1)) {
                resultMap.put(val, new Node(val, val));
                return;
            }
            // 大于的，但是不独立的
            // 可以接在前面
            if (lowerNode.end < val && resultMap.containsKey(val + 1)) {
                resultMap.put(val, new Node(val, resultMap.get(val + 1).end));
                resultMap.remove(resultMap.get(val + 1).start);
                return;
            }
        }
        System.out.println();

    }

    public int[][] getIntervals() {
        int length = resultMap.size();
        int[][] ans = new int[length][2];
        int i = 0;
        Set<Integer> keys = resultMap.keySet();
        Iterator<Integer> iterator = keys.iterator();
        while (iterator.hasNext()) {
            Integer key = iterator.next();
            Node node = resultMap.get(key);
            if (node.start == node.end) {
                ans[i][0] = node.start;
                ans[i][1] = node.end;
            } else {
                ans[i][0] = node.start;
                ans[i][1] = node.end;
            }
            i++;
        }


        return ans;
    }

    public void print(int[][] ans) {
        for (int i = 0; i < ans.length; i++) {
            System.out.print("[");
            for (int j = 0; j < ans[i].length; j++) {
                System.out.print(ans[i][j] + " ");
            }
            System.out.println("]");
        }
    }
    public static class Comp implements Comparator<Integer>{

        @Override
        public int compare(Integer o1, Integer o2) {
            return Integer.valueOf(o1) - Integer.valueOf(o2);
        }
    }
}
